package com.htbb.common.interceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.util.StringUtils;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import com.htbb.common.base.MappingConfig;
import com.htbb.common.base.exception.BizException;
import com.htbb.common.util.CheckIdCardUtils;
import com.htbb.common.util.MD5Util;
import com.htbb.model.personalInsure.dto.Channel;

/**
 * @date 2016-12-27
 * @description 身份认证拦截器
 * @author wangzhicheng
 *
 */
public class AuthenticationInterceptor implements HandlerInterceptor {

	private final Logger logger = LoggerFactory.getLogger(this.getClass());

	public static void main(String[] args) {
		//String md5 = MD5Util.MD5Encode("qbw1234567890362402199104111039", null);
		String md5 = MD5Util.MD5Encode("qbw1234567890116", null);
		System.out.println(md5);
	}
	/**
	 * MD5加密
	 * 
	 * @param channel
	 * @param certificateCode
	 * @return
	 */
	private String getSign(Channel channel, String certificateCode) {
		return MD5Util.MD5Encode(channel.getUser() + channel.getPassword() + certificateCode, null);
	}

	public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
			throws Exception {
		logger.info("~~~~~~~~~~~~~~~~~~~~~AuthenticationInterceptor  start~~~~~~~~~~~~~~~");
		String user = request.getParameter("user");
		//String certificateCode = request.getParameter("certificateCode");
		String partnerCode = request.getParameter("partnerCode");
		String sign = request.getParameter("sign");
		if (StringUtils.isEmpty(partnerCode)) {
			throw new BizException("the partnerCode param is null.");
		}
		/**
		 * 校验参数，首先判断参数是否为空，先判断partnerCode，根据partnerCode看是否能够得到渠道配置信息，
		 * 如果配置信息为空，则说明partnerCode错误，
		 * 包括验证身份证号格式，最终，将sign与参数加密后的sign比较看是否相等，如果不等，说明身份验证失败。
		 */
//		Channel channel = MappingConfig.channelConfig.get(partnerCode.toUpperCase());
		Channel channel = MappingConfig.getChannel(partnerCode.toUpperCase());
		if (channel == null) {
			throw new BizException("the partnerCode param is error.");
		}
		if (StringUtils.isEmpty(user)) {
			throw new BizException("the user param is null.");
		} else if (!user.equals(channel.getUser())) {
			throw new BizException("the user param is error.");
		}
		/*if (certificateCode == null) {
			throw new BizException("the certificateCode param is null.");
		} else if (!CheckIdCardUtils.validateCard(certificateCode)) {
			throw new BizException("the certificateCode param is error");
		}*/
		
		if (StringUtils.isEmpty(sign)) {
			throw new BizException("the sign param is null");
		} else if (!sign.equalsIgnoreCase(getSign(channel, partnerCode))) {
			throw new BizException("身份验证失败，请重新提交！");
		}
		return true;
	}

	public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler,
			ModelAndView modelAndView) throws Exception {

	}

	public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex)
			throws Exception {

	}

}
